package com.coocaa.ccmvp.demo.v;

import android.support.test.espresso.Espresso;
import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.support.test.rule.UiThreadTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.coocaa.ccmvp.MainActivity;
import com.coocaa.ccmvp.R;
import com.coocaa.ccmvp.demo.p.DemoPresenter;
import com.coocaa.ccmvp.demo.p.IDemoPresenter;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;

import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;

/**
 * Created by lu on 17-2-7.
 */

@RunWith(AndroidJUnit4.class)
public class IDemoViewTest {
    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
            MainActivity.class);
    @Rule
    public UiThreadTestRule mUiThreadRule = new UiThreadTestRule();

    private IDemoView demoView = null;

    @Before
    public void setUp() throws Exception {
        demoView = new DemoView();
        demoView.create(mActivityRule.getActivity());
    }

    @Test
    public void performClickLoginButton() throws Exception {
        demoView.showMe(mActivityRule.getActivity().getFragmentManager());
        IDemoPresenter demoPresenter = Mockito.mock(DemoPresenter.class);
        demoView.attachPresenter(demoPresenter);
        final String name = "name_123321";
        final String passwd = "passwd_321123";
        Espresso.onView(ViewMatchers.withId(R.id.layout_login_dailog_editText_name))
                .perform(ViewActions.typeText(name), ViewActions.closeSoftKeyboard());
        Thread.sleep(3 * 1000);
        Espresso.onView(ViewMatchers.withId(R.id.layout_login_dailog_editText_passwd))
                .perform(ViewActions.typeText(passwd), ViewActions.closeSoftKeyboard());
        Thread.sleep(3 * 1000);
        Espresso.onView(ViewMatchers.withId(R.id.layout_login_dailog_button_login))
                .perform(ViewActions.click());
        Mockito.verify(demoPresenter).login(name, passwd);
        Thread.sleep(3 * 1000);

        demoView.hideMe();
    }

    @Test
    public void showSomeTips() throws Throwable {
        final String tips1 = "1233211234567";
        mUiThreadRule.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                demoView.showTips(tips1);
                // Espresso.onView(ViewMatchers.withText("11")).check(matches(isDisplayed()));
            }
        });
        Espresso.onView(withText(tips1))
                .inRoot(withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView()))))
                .check(matches(isDisplayed()));
    }
}
